package com.zenika.zenbinding.client.handler;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.zenika.zenbinding.client.model.ValueModel;

/**
 * @author Pierre Queinnec
 */
public class TextAreaHandler extends ValueBindingHandler implements ChangeHandler {
  
  private ValueModel model;
  private TextArea textArea;
  
  public TextAreaHandler(TextArea textArea, ValueModel model) {
    super();
    this.textArea = textArea;
    this.model = model;
    this.model.setHandler(this);
    this.bind();
  }
  
  public Object getValue() {
    return this.textArea.getText();
  }
  
  public Widget getWidget() {
    return this.textArea;
  }
  
  public void onChange(ChangeEvent event) {
    this.fireChange(this.textArea.getText(), this);
  }
  
  public void setErrorStyle() {
    this.textArea.setStyleName("error-TextArea");
  }
  
  public void setRegularStyle() {
    this.textArea.setStyleName("gwt-TextArea");
  }
  
  private void bind() {
    this.textArea.setText(this.model.getValue());
    this.textArea.addChangeHandler(this);
  }
  
}
